package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.project.sys.entity.SysMaterialClassification;
import com.ctcemti.mdm.project.sys.entity.formdata.BaseReq;
import com.ctcemti.mdm.project.sys.service.ISysMaterialClassificationService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import tk.mybatis.mapper.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Author bxf
 * @Description 物资分类表控制器层
 * @Date 14:32 2019/10/28
 * @Param 
 * @return 
 **/
@RestController
@Api(value = "SysMaterialClassificationController", description = "物资分类控制器类")
@RequestMapping("sysMaterialClassification")
public class SysMaterialClassificationController {
    
    @Autowired
    private ISysMaterialClassificationService sysMaterialClassificationService;

    @ApiOperation(value = "根据物资分类id或者名称获取子级(异步加载)")
    @PostMapping("selectSysMaterialClassificationByPid")
    @ApiImplicitParam(name="id",value = "父级id")
    public Result<List<SysMaterialClassification>> selectByPid(HttpServletRequest request, HttpServletResponse response , String code, String name){
        List<SysMaterialClassification> list = new ArrayList<SysMaterialClassification>();
        if(StringUtil.isEmpty(code)){
            SysMaterialClassification SysMaterialClassification = new SysMaterialClassification();
            SysMaterialClassification.setId(-1);
            SysMaterialClassification.setName("物资分类");
            SysMaterialClassification.setCode("-1");
            SysMaterialClassification.setParent(true);
            SysMaterialClassification.setOpen(true);
            if(StringUtil.isEmpty(name)){
                list = sysMaterialClassificationService.selectByPid("-1");
                list.add(SysMaterialClassification);
            }else{
                list = sysMaterialClassificationService.selectSysMaterialClassificationByName(name);
                list.add(SysMaterialClassification);
            }

        }else{
            list = sysMaterialClassificationService.selectByPid(code);
        }

        return new Result<List<SysMaterialClassification>>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "分页物资分类")
    @PostMapping("selectListByPid")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value = "父级id"),@ApiImplicitParam(name="SysMaterialClassification",value = "物资分类实体类"),@ApiImplicitParam(name="id",value = "父级id")})
    public Result<PageInfo<SysMaterialClassification>> selectListByPid(HttpServletRequest request, HttpServletResponse response , String id, BaseReq baseReq){

        PageInfo<SysMaterialClassification> pageInfo = sysMaterialClassificationService.selectListByPid(id,baseReq);
        return new Result<PageInfo<SysMaterialClassification>>(ResultEnum.SUCCESS,pageInfo);
    }


    @ApiOperation(value = "保存物资分类")
    @PostMapping("insertSysMaterialClassification")
    @ApiImplicitParam(name="SysMaterialClassification",value = "物资分类实体类")
    public Result<SysMaterialClassification> insertSysMaterialClassification(HttpServletRequest request, HttpServletResponse response , SysMaterialClassification SysMaterialClassification){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        SysMaterialClassification.setCreator(loginId);
        sysMaterialClassificationService.insertSelective(SysMaterialClassification);
        return new Result<SysMaterialClassification>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "更新物资分类")
    @PostMapping("updateSysMaterialClassification")
    @ApiImplicitParam(name="SysMaterialClassification",value = "物资分类实体类")
    public Result<SysMaterialClassification> updateSysMaterialClassification(HttpServletRequest request, HttpServletResponse response, SysMaterialClassification SysMaterialClassification){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        SysMaterialClassification.setModifiedBy(loginId);
        SysMaterialClassification.setModifyDate(new Date());
        sysMaterialClassificationService.updateByIdSelective(SysMaterialClassification);
        return new Result<SysMaterialClassification>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "获取所有的物资分类子目")
    @PostMapping("selectAllSysMaterialClassification")
    public Result<List<SysMaterialClassification>> selectAllSysMaterialClassification(HttpServletRequest request, HttpServletResponse response){
        List<SysMaterialClassification> list = new ArrayList<SysMaterialClassification>();
        list = sysMaterialClassificationService.selectAllSysMaterialClassification();
        return new Result<List<SysMaterialClassification>>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "根据物资分类属性获取list")
    @PostMapping("selectSysMaterialClassificationByEntity")
    @ApiImplicitParam(name="SysMaterialClassification",value = "物资分类实体类")
    public Result selectSysMaterialClassificationByEntity(HttpServletRequest request, HttpServletResponse response,SysMaterialClassification entity){
        List<SysMaterialClassification> list = new ArrayList<SysMaterialClassification>();
        list = sysMaterialClassificationService.selectSysMaterialClassification(entity);
        return new Result<>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "根据ids级联删除物资分类")
    @PostMapping("deleteSysMaterialClassificationByIds")
    //@ApiImplicitParams({@ApiImplicitParam(name="id", name="主键id"), @ApiImplicitParam(name="permission", name="当前按钮权限")})
    @ApiImplicitParam(name="SysMaterialClassification",value = "物资分类实体类")
    public Result<SysMaterialClassification> deleteSysMaterialClassificationByIds(HttpServletRequest request, HttpServletResponse response,String ids){
        List<SysMaterialClassification> list = new ArrayList<SysMaterialClassification>();
        int flag = sysMaterialClassificationService.deleteByPrimaryKeys(ids);
        return new Result<SysMaterialClassification>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "数据导入")
    @PostMapping("uploadFile")
    public Result<Map<String,Object>> uploadFile(HttpServletRequest request, HttpServletResponse response, String category) throws Exception {
        CommonsMultipartResolver commonsMultipartResolver =  new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartFile multipartFile = null;
        if(commonsMultipartResolver.isMultipart(request)){
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
            Iterator<String> iterator = multipartHttpServletRequest.getFileNames();
            while (iterator.hasNext()){
                multipartFile= multipartHttpServletRequest.getFile(iterator.next());
            }
        }
        Map<String,Object> result = sysMaterialClassificationService.uploadFile(multipartFile,category,response,request);
        return new Result<Map<String,Object>>(ResultEnum.SUCCESS,result);
    }
}